Skip to content

Comments

chore: improve mock asset controller data source tests#7958

Merged
Prithpal-Sooriya merged 3 commits intomainfrom
refactor/clean-up-data-source-tests
Feb 18, 2026
Merged

chore: improve mock asset controller data source tests#7958
Prithpal-Sooriya merged 3 commits intomainfrom
refactor/clean-up-data-source-tests

Conversation

@Prithpal-Sooriya
Copy link
Contributor

@Prithpal-Sooriya Prithpal-Sooriya commented Feb 17, 2026

Explanation

This commit introduces a new mock implementation of the AssetControllerMessenger, which facilitates testing by simulating various actions and events related to asset management. The mock includes methods for creating a mock provider, registering action handlers, and simulating network states. Additionally, it updates the RpcDataSource and StakedBalanceDataSource tests to utilize the new mock, improving test reliability and coverage.

Refactors existing tests to remove unnecessary complexity and improve clarity, ensuring that the mock accurately reflects the expected behavior of the real messenger in a controlled environment.

References

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Note

Medium Risk
Mostly test-only refactors, but it changes StakedBalanceDataSource subscription teardown behavior to rely on messenger-level cleanup, which could affect consumers with custom messenger implementations.

Overview
Improves assets-controller data source tests by introducing a shared MockAssetControllerMessenger fixture that delegates the relevant actions/events and provides helpers for registering RPC/staking handlers and mocking a Web3Provider.

Updates RpcDataSource and StakedBalanceDataSource tests to use the shared fixture, simplify network-state/event publishing via the root messenger, and tighten event-driven staking refresh assertions. Also exports STAKING_INTERFACE for reuse in mocks and makes small type-safety cleanups in RpcDataSource around state/token-list access and native asset ID construction.

Written by Cursor Bugbot for commit b2c402c. This will update automatically on new commits. Configure here.

This commit introduces a new mock implementation of the AssetControllerMessenger, which facilitates testing by simulating various actions and events related to asset management. The mock includes methods for creating a mock provider, registering action handlers, and simulating network states. Additionally, it updates the RpcDataSource and StakedBalanceDataSource tests to utilize the new mock, improving test reliability and coverage.

Refactors existing tests to remove unnecessary complexity and improve clarity, ensuring that the mock accurately reflects the expected behavior of the real messenger in a controlled environment.
@Prithpal-Sooriya Prithpal-Sooriya requested a review from a team as a code owner February 17, 2026 17:32

// Test escape hatch for mocking areas that do not need explicit types
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type TestMockType = any;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only 1 or 2 places are using the escape hatch 🤞🏾

Comment on lines +49 to +97
rootMessenger.delegate({
messenger: assetsControllerMessenger,
actions: [
// AssetsController
'AccountTreeController:getAccountsFromSelectedAccountGroup',
// RpcDataSource
'TokenListController:getState',
'NetworkController:getState',
'NetworkController:getNetworkClientById',
// RpcDataSource, StakedBalanceDataSource
'NetworkEnablementController:getState',
// SnapDataSource
'SnapController:getRunnableSnaps',
'SnapController:handleRequest',
'PermissionController:getPermissions',
// BackendWebsocketDataSource
'BackendWebSocketService:connect',
'BackendWebSocketService:disconnect',
'BackendWebSocketService:forceReconnection',
'BackendWebSocketService:sendMessage',
'BackendWebSocketService:sendRequest',
'BackendWebSocketService:getConnectionInfo',
'BackendWebSocketService:getSubscriptionsByChannel',
'BackendWebSocketService:channelHasSubscription',
'BackendWebSocketService:findSubscriptionsByChannelPrefix',
'BackendWebSocketService:addChannelCallback',
'BackendWebSocketService:removeChannelCallback',
'BackendWebSocketService:getChannelCallbacks',
'BackendWebSocketService:subscribe',
],
events: [
// AssetsController
'AccountTreeController:selectedAccountGroupChange',
'KeyringController:lock',
'KeyringController:unlock',
'PreferencesController:stateChange',
// RpcDataSource, StakedBalanceDataSource
'NetworkController:stateChange',
'TransactionController:transactionConfirmed',
'TransactionController:incomingTransactionsReceived',
// StakedBalanceDataSource
'NetworkEnablementController:stateChange',
// SnapDataSource
'AccountsController:accountBalancesUpdated',
'PermissionController:stateChange',
// BackendWebsocketDataSource
'BackendWebSocketService:connectionStateChanged',
],
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah lets def sync on the AssetController messenger.
This messenger has way too many responsibilities.

Ideally each data source should have its own responsibilities.

Comment on lines +65 to +77
'BackendWebSocketService:connect',
'BackendWebSocketService:disconnect',
'BackendWebSocketService:forceReconnection',
'BackendWebSocketService:sendMessage',
'BackendWebSocketService:sendRequest',
'BackendWebSocketService:getConnectionInfo',
'BackendWebSocketService:getSubscriptionsByChannel',
'BackendWebSocketService:channelHasSubscription',
'BackendWebSocketService:findSubscriptionsByChannelPrefix',
'BackendWebSocketService:addChannelCallback',
'BackendWebSocketService:removeChannelCallback',
'BackendWebSocketService:getChannelCallbacks',
'BackendWebSocketService:subscribe',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the asset controller really need ALL the backend actions?

@Prithpal-Sooriya Prithpal-Sooriya requested a review from a team as a code owner February 17, 2026 17:43
…DataSource

This commit cleans up the `destroy` method in the `StakedBalanceDataSource` class by removing the unsubscription logic for transaction and network events, which is no longer necessary. Corresponding test cases have also been removed to reflect this change.
this.#handleStakedBalanceUpdate.bind(this),
);

const unsubConfirmed = this.#messenger.subscribe(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.subscribe does not return a callback, so this was not needed.

Comment on lines -922 to -925
this.#unsubscribeTransactionConfirmed?.();
this.#unsubscribeIncomingTransactions?.();
this.#unsubscribeNetworkStateChange?.();
this.#unsubscribeNetworkEnablementControllerStateChange?.();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no callback to unsubscribe from.

});
});

describe('destroy', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test is irrelevant. The controller destruction will clear messenger subscriptions.

BUT we share this messenger with other data sources, so a data source destruction might impact other data sources 💀

We can discuss delegating and generating diff messengers per data source.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Copy link
Contributor

@salimtb salimtb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested and work as expected

@Prithpal-Sooriya Prithpal-Sooriya added this pull request to the merge queue Feb 18, 2026
Merged via the queue into main with commit c3169f5 Feb 18, 2026
306 checks passed
@Prithpal-Sooriya Prithpal-Sooriya deleted the refactor/clean-up-data-source-tests branch February 18, 2026 09:55
khanti42 pushed a commit that referenced this pull request Feb 20, 2026
## Explanation

This commit introduces a new mock implementation of the
AssetControllerMessenger, which facilitates testing by simulating
various actions and events related to asset management. The mock
includes methods for creating a mock provider, registering action
handlers, and simulating network states. Additionally, it updates the
RpcDataSource and StakedBalanceDataSource tests to utilize the new mock,
improving test reliability and coverage.

Refactors existing tests to remove unnecessary complexity and improve
clarity, ensuring that the mock accurately reflects the expected
behavior of the real messenger in a controlled environment.

## References

<!--
Are there any issues that this pull request is tied to?
Are there other links that reviewers should consult to understand these
changes better?
Are there client or consumer pull requests to adopt any breaking
changes?

For example:

* Fixes #12345
* Related to #67890
-->

## Checklist

- [x] I've updated the test suite for new or updated code as appropriate
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [x] I've communicated my changes to consumers by [updating changelogs
for packages I've
changed](https://github.com/MetaMask/core/tree/main/docs/processes/updating-changelogs.md)
- [x] I've introduced [breaking
changes](https://github.com/MetaMask/core/tree/main/docs/processes/breaking-changes.md)
in this PR and have prepared draft pull requests for clients and
consumer packages to resolve them

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Medium Risk**
> Mostly test-only refactors, but it changes `StakedBalanceDataSource`
subscription teardown behavior to rely on messenger-level cleanup, which
could affect consumers with custom messenger implementations.
> 
> **Overview**
> Improves `assets-controller` data source tests by introducing a shared
`MockAssetControllerMessenger` fixture that delegates the relevant
actions/events and provides helpers for registering RPC/staking handlers
and mocking a `Web3Provider`.
> 
> Updates `RpcDataSource` and `StakedBalanceDataSource` tests to use the
shared fixture, simplify network-state/event publishing via the root
messenger, and tighten event-driven staking refresh assertions. Also
exports `STAKING_INTERFACE` for reuse in mocks and makes small
type-safety cleanups in `RpcDataSource` around state/token-list access
and native asset ID construction.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
b2c402c. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants